package com.eric.ztea.controller;

import java.util.List;
import java.util.ArrayList;

import javax.validation.Valid;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.eric.ztea.dao.SysUserRepository;
import com.eric.ztea.domain.SysRole;
import com.eric.ztea.domain.SysUser;
//import com.eric.ztea.form.SysUserForm;

@RestController
@RequestMapping("/api/user")
public class SysUserController {	
	
	@Autowired
	SysUserRepository userRepository;
	
	@GET
	@RequestMapping(value = "/list")
	public ModelAndView  showUsers() {
		List<SysUser> users = userRepository.findAll();
		System.out.println(">>user>>"+users.size());
		ModelAndView mav = new ModelAndView();  
		mav.setViewName("userlist");  
//		mav.addObject("animalForm", new AnimalForm());  
		mav.addObject("userlist", users);  
		return mav;
	}
	
	@POST
	@RequestMapping(path = "/list", params = {"save"})  
    public ModelAndView doAdd(ModelAndView mav, @Valid SysUser sysUser, BindingResult result){   
        System.out.println("用户名：" + sysUser.getUsername());  
        System.out.println("密码：" + sysUser.getPassword());  
        if(result.hasErrors()){  
        	mav.addObject("MSG", "出错啦！");  
        }else{  
        	mav.addObject("MSG", "提交成功！");  
        }
        SysUser user = userRepository.save(new SysUser(null, sysUser.getUsername(), 
        		sysUser.getPassword(), null, null, null, null, null, null));
        mav.setViewName("userlist"); 
        mav.addObject("user", user);
        
        return mav;  
    }
	
	@GET
	@RequestMapping(value = "/findByName")
	public SysUser findByName(@RequestParam(value = "name", required = true) String name) {
		return userRepository.findByUsername(name);
	}

	@GET
	@RequestMapping(value = "/findByTelephone")
	public SysUser findByTelephone(@RequestParam(value = "telephone", required = true) String telephone) {
		return userRepository.findByTelephone(telephone);
	}

	@GET
	@RequestMapping(value = "/findById")
	public SysUser findById(@RequestParam(value = "id", required = true) Long id) {
		return userRepository.findById(id);
	}
	
	@POST
	@RequestMapping(value = "/save")
	public SysUser save(@RequestParam(value = "id", required = false) Long id,
			@RequestParam(value = "openid", required = true) String openid,
			@RequestParam(value = "username", required = true) String username,
			@RequestParam(value = "password", required = false) String password,
			@RequestParam(value = "telephone", required = false) String telephone,
			@RequestParam(value = "icon", required = false) String icon,
			@RequestParam(value = "nickname", required = false) String nickname) {
		
		
		List<SysRole> roles = new ArrayList<SysRole>();
		SysRole role = new SysRole();
		role.setId(Long.valueOf(2));
		role.setName("ROLE_USER");
		roles.add(role);
		
		return userRepository.save(new SysUser(id,username,password,openid,telephone, Double.valueOf(0), icon, nickname, roles));
	}
	
	@DELETE
	@RequestMapping(value = "/delete")
	public void delete(@RequestParam(value = "id", required = true) Long id)
	{
		userRepository.delete(id);
	}
	
}
